<template>
  <div class="login-wrap">
    <div class="login-contain">
      <div class="login-header">
        <p></p>
      </div>
      <div class="form-group">
        <div class="form-item">
          <label for="username">
            <img src="../../assets/loginImg/iphone2.png" />
          </label>
          <input
            id="username"
            type="text"
            maxlength="11"
            placeholder="请输入手机号"
            v-model="iphone"
            @input="iph"
          />
          <img :src="isok == true ? yes : err" alt="" v-show="img" />
        </div>
        <div class="form-item dis-get">
          <label for="password">
            <img src="../../assets/loginImg/duanxin.png" alt="" />
          </label>
          <input
            id="iphoneCode"
            type="text"
            maxlength="6"
            placeholder="请输入验证码"
            v-model="Numphone"
          />
          <span class="getNum-iphone" @click="getCode()"
            >获取验证码 <b>{{ count }}</b></span
          >
        </div>
      </div>
      <div class="button-group">
        <van-button type="default" class="login-btn" id="doLogin" @click="doLoginIphone()">登录</van-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import { Toast } from "vant";
export default {
  name: "WorkspaceJsonLogin",

  data() {
    return {
      img: false,
      iphone: "",
      Numphone:'',
      isok: false,
      yes: require("../../assets/loginImg/yes.png"),
      err: require("../../assets/loginImg/err.png"),
      // 验证码
      count: "",
      timer: null,
    };
  },

  mounted() {},

  methods: {
    // 手机验证
    iph() {
      this.img = true;
      let userReg = new RegExp(
        "^1[3-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"
      );
      if (this.iphone == "") {
        this.isok = false;
      } else if (userReg.test(this.iphone)) {
        this.isok = true;
      } else {
        this.isok = false;
      }
    },

    // 倒计时
    getCode() {
      if (this.isok == true) {
        const TIME_COUNT = 60;
        if (!this.timer) {
          this.count = TIME_COUNT;
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= TIME_COUNT) {
              this.count--;
            } else {
              clearInterval(this.timer);
              this.timer = null;
              this.count = "";
            }
          }, 1000);

          // 倒计时开始发送手机号
          this.$http.post('/smsIphone',{
            iphone:this.iphone
          }).then(res=>{
            if(res.data.code==202){
               // 未绑定该邮箱后清除定时器
              Toast.fail('该手机号未注册')
              clearInterval(this.timer);
              this.timer = null;
              this.count = ''
            }else{
              Toast.fail('发送成功')
            }
            console.log(res);
          }).catch(err=>{
            Toast.fail('发送失败，请稍后重试')
          })

        }
      }else {
        Toast.fail("请输入格式正确的手机号");
      }
    },
    // 验证码发送后点击登录
    doLoginIphone(){
      if(this.iphone=='' || this.Numphone=='' || this.isok==false){
        Toast.fail('请完成表单验证')
      }else{
        this.$http.post('/iphoneLogin',{
          iphone:this.iphone,
          iphoneId:this.Numphone
        }).then(res=>{
          console.log(res);
          if(res.data.code == 200){
            Toast.success('登录成功')
             // 用户名加密存储
            let encodedData = window.btoa(window.encodeURIComponent(res.data.data[0].uname));
            sessionStorage.setItem('cookie-session',encodedData)
            Toast.success('登录成功');
            this.$router.replace('/me')

          }else if(res.data.code == 203){
            Toast.fail('验证码错误')
          }
        }).catch(err=>{
          Toast.fail('登录失败，请稍后重试')
        })
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.login-wrap {
  background: url("../..//assets/loginImg/bac-bg.jpg") no-repeat;
  background-size: cover;

  /* background: linear-gradient(rgba(248, 248, 248,0.1), rgba(248, 248, 248,0.1)), url('../assets/loginImg/bac-bg.jpg') no-repeat; */
}

a,
li {
  list-style: none;
}
a {
  text-decoration: none;
  color: black;
}

.login-contain {
  margin: 0 !important;
  width: 100%;
  min-height: 800px;
}
.login-header {
  padding: 5%;
}
.login-header p {
  margin: 150px 0;
  font-size: 32px;
  color: #ffffff;
  font-weight: bold;
  text-align: center;
  letter-spacing: 2px;
  text-shadow: 0.1em 0.15em 0.1em #55a6ad;
}
.login-logo {
  padding: 5%;
}
.login-logo img {
  width: 55px;
  height: 55px;
  border-radius: 50%;
}
.login-logo p {
  color: #ffffff;
  font-size: 14px;
  text-align: center;
  letter-spacing: 2px;
  margin-top: 2%;
}
.form-group {
  padding: 5%;
}
.form-group .form-item {
  margin-top: 5%;
  padding: 0 10px;
  border-radius: 20px;
  background-color: rgba(59, 208, 245, 0.5);
  display: flex;
  justify-content: flex-start;
  > img {
    margin: 10px 0 0 15px;
    width: 25px;
    height: 25px;
  }
}
.form-group .form-item input {
  outline: none;
  border: 0;
  background-color: transparent;
  color: #000000;
  height: 40px;
  font-size: 18px;
  width: 70%;
  margin-left: 12%;
}
.form-group .form-item input::-webkit-input-placeholder {
  /* WebKit browsers */
  color: #ffffff;
  font-size: 16px;
}
.form-group .form-item input:-moz-placeholder {
  /* Mozilla Firefox 4 to 18 */
  color: #ffffff;
  font-size: 16px;
}
.form-group .form-item input::-moz-placeholder {
  /* Mozilla Firefox 19+ */
  color: #ffffff;
  font-size: 16px;
}
.form-group .form-item input:-ms-input-placeholder {
  /* Internet Explorer 10+ */
  color: #ffffff;
  font-size: 16px;
}
.form-group .form-item label img {
  width: 25px;
  position: absolute;
  margin-top: 5px;
}
.form-group .form-item button {
  outline: none;
  background: transparent;
  border: 1px #00cc99 dashed;
  color: #ffffff;
  height: 30px;
  border-radius: 5px;
  float: right;
  padding: 1%;
}
.button-group {
  padding: 5%;
}
.button-group button {
  outline: none;
  border: 0;
  width: 90%;
  height: 40px;
  margin-top: 4%;
  border-radius: 20px;
  margin-left: 4%;
  color: #ffffff;
  font-size: 18px;
}
.button-group .login-btn {
  background-color: #ffffff;
  color: #74c3ca;
}

.order-login {
  padding: 5%;
}
.order-login-line {
  display: block;
  position: relative;
  text-align: center;
  font-size: 14px;
  color: #ffffff;
}
.order-login-line:before,
.order-login-line:after {
  content: "";
  position: absolute;
  top: 50%;
  background: #ffffff;
  width: 20%;
  height: 1px;
}
.order-login-line:before {
  left: 10%;
}
.order-login-line:after {
  right: 10%;
}
.order-login-box {
  display: flex;
  width: 100%;
  justify-content: center;
  margin-top: 20px;
}
.order-login-box div {
  flex: 1;
  text-align: center;
}
.order-login-box div p {
  text-align: center;
  font-size: 14px;
  color: #ffffff;
}
.display {
  a {
    margin: 0 30px;
  }
  display: flex;
  justify-content: center;
}
.dis-get {
  display: flex;
}
#iphoneCode {
  width: 50%;
}
.getNum-iphone {
  line-height: 42px;
  text-align: center;
  margin-left: 20px;
}
</style>